package org.example.tree;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.support.odps.udf.CodecCheck;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
    private int pid;
    private String name;
    private int parentId;
    private String key;
    private int mask;
    private List<TreeNode> childNodes;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getMask() {
        return mask;
    }

    public void setMask(int mask) {
        this.mask = mask;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public List<TreeNode> getChildNodes() {
        return childNodes;
    }

    public void setChildNodes(List<TreeNode> childNodes) {
        this.childNodes = childNodes;
    }

    public static List<TreeNode>  buildTree(List<TreeNode> list,int parentId,int count){
        List<TreeNode> menus=new ArrayList<>();

        for (TreeNode node:list) {
            int treeId=node.getPid();
            int treeParentId=node.getParentId();
            if(treeParentId==parentId){
                count++;
              List<TreeNode> nodes=buildTree(list,treeId,count);
              node.setChildNodes(nodes);
              List<String> listStr=new ArrayList<>();
              nodes.forEach(n->{
                  listStr.add(n.getKey());
              });
              if(listStr.size()>0){
                  System.out.println("key的对应关系\t 第"+count+"层：\t"+node.getKey()+":"+listStr.toString());
              }
              menus.add(node);
            }
        }
        return menus;
    }

    public static void main(String[] args) {
        TreeNode node1=new TreeNode();
        node1.setName("父节点1");
        node1.setPid(1);
        node1.setParentId(0);
        node1.setKey("key1");

        TreeNode node2=new TreeNode();
        node2.setName("父节点1-子节点1");
        node2.setPid(2);
        node2.setParentId(1);
        node2.setKey("key2");

        TreeNode node3=new TreeNode();
        node3.setName("父节点1-子节点1-子子节点1");
        node3.setPid(3);
        node3.setParentId(2);
        node3.setKey("key3");

        TreeNode node4=new TreeNode();
        node4.setName("父节点2");
        node4.setPid(4);
        node4.setParentId(0);
        node4.setKey("key4");

        TreeNode node5=new TreeNode();
        node5.setName("父节点2-子节点1");
        node5.setPid(5);
        node5.setParentId(4);
        node5.setKey("key5");

        TreeNode node6=new TreeNode();
        node6.setName("父节点2-子节点1-子子节点1");
        node6.setPid(6);
        node6.setParentId(5);
        node6.setKey("key6");

        TreeNode node7=new TreeNode();
        node7.setName("父节点2-子节点1-子子节点2");
        node7.setPid(7);
        node7.setParentId(5);
        node7.setKey("key7");

        TreeNode node8=new TreeNode();
        node8.setName("父节点2-子节点1-子子节点3");
        node8.setPid(7);
        node8.setParentId(5);
        node8.setKey("key8");


        List<TreeNode> list = new ArrayList<>();
        list.add(node1);
        list.add(node2);
        list.add(node3);
        list.add(node4);
        list.add(node5);
        list.add(node6);
        list.add(node7);
        list.add(node8);

        List<TreeNode> treeNodes = buildTree(list,0,0);
        String jsonStr=JSON.toJSONString(treeNodes);
        System.out.println("树形结构为："+jsonStr);





    }
}
